<template>
  <div class="register-container">
      <div class="register-box">
          <h3 style="margin-left: 220px;">账号注册</h3>
          <el-form :model="userForm" status-icon :rules="rules" ref="userForm" label-width="100px" class="demo-ruleForm">

              <div class="register-box-left">
                  <el-upload class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/"
                      :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                      <img v-if="userForm.imageIcon" :src="userForm.imageIcon" class="avatar">
                      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                  </el-upload>
              </div>

              <div class="register-box-right">
                  <el-form-item prop="email" label="" :rules="[
                      { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                      { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
                  ]">
                      <el-input v-model="userForm.uid" placeholder="邮箱"></el-input>
                  </el-form-item>
                  <el-form-item label="" prop="password">
                      <el-input type="password" v-model="userForm.password" autocomplete="off"
                          placeholder="密码"></el-input>
                  </el-form-item>
                  <el-form-item label="" prop="checkPass">
                      <el-input type="password" v-model="userForm.checkPass" autocomplete="off"
                          placeholder="确认密码"></el-input>
                  </el-form-item>

                  <el-form-item>
                      <p style="font-size: 10px;margin-top: -15px;margin-bottom: -15px;">
                          *已阅读并同意 <a href="#">平台用户协议</a> 和 <a href="#">隐私政策</a>
                      </p>
                  </el-form-item>
                  <el-form-item>
                      <el-ow>
                          <el-button type="primary" @click="submitForm('userForm')">提交</el-button>
                          <el-button @click="resetForm('userForm')">重置</el-button>
                      </el-ow>

                  </el-form-item>
              </div>

          </el-form>
      </div>
  </div>
</template>

<script>
export default {
  name: 'RegisterIndex',
  data () {
    return {
      userForm: {
        uid: '', // 账号
        password: '', // 密码
        checkPass: '', // 确认密码
        imageIcon: '' // 用户头像链接
      },
      rules: {
        uid: [
          { required: true, message: '请输入邮箱账号', trigger: 'blur' },
          { type: 'email', message: '请输入有效的邮箱账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
        ],
        checkPass: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: this.validateConfirmPassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.userForm.password !== this.userForm.checkPass) {
            this.$message.error('两次输入的密码不一致')
            return
          }

          this.$message.success('注册成功')
          // 可以进行路由跳转或其他操作
        }
      })
    },
    validateConfirmPassword (rule, value, callback) {
      if (value !== this.userForm.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    },
    handleAvatarSuccess (res, file) {
      this.userForm.imageIcon = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url(../../assets/img/loginBG.png);
  background-size: cover;
}

.register-box {
  width: 550px;
  padding: 10px;
  background-color: #f5f5f5;
  border-radius: 5px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: absolute;
  left: 60%;
  top: 40%;

}

.avatar-uploader .el-upload {
  border: 1px dashed black;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 24px;
  color: #8c939d;
  width: 130px;
  height: 130px;
  line-height: 130px;
  text-align: center;
  background-color: #fff;
}

.avatar {
  width: 130px;
  height: 130px;
  display: block;
}

/* Adjust input and button width */
.el-input {
  width: 240px;
}

.el-button {
  width: 100px;
}
.register-box-right{
  margin-left: 30%;
  margin-top: -25%;

}
.register-box-left{
  margin-left: 10%;
  margin-top: 10%;
}
</style>
